package com.example.demo.java8;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.*;

/**
 * @ClassName DateTimeFormatterTest
 * @Description TODO
 * @Author hly
 * @Date 2018\11\29 0029 15:01
 */
public class DateTimeFormatterTest {

    //调用普通时间 API SimpleDateFormat 实现时间格式化

    public void test01() throws InterruptedException, ExecutionException {
        // 构建时间格式化实例
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        // 构建一个时间任务
        Callable<Date> task = new Callable<Date>() {

            @Override
            public Date call() throws Exception {

                return sdf.parse("20180427");
            }
        };




        // 构建一个线程池
        ExecutorService pool = Executors.newFixedThreadPool(100);

        List<Future<Date>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(pool.submit(task));
        }

        for (Future<Date> future : list) {
            System.out.println(future.get());
        }
        pool.shutdown();
    }

    public void test02() throws InterruptedException, ExecutionException{

//        Callable<Date> tesk = new Callable<Date>() {
//
//            @Override
//            public Date call() throws Exception {
//                return FormatThreadLocal.convert("20180427");
//            }
//        };
//
//        ExecutorService pool = Executors.newFixedThreadPool(10);
//
//        List<Future<Date>> list = new ArrayList<>();
//        for (int i = 0; i < 10; i++) {
//            list.add(pool.submit(tesk));
//        }
//
//        for (Future<Date> future : list) {
//            System.out.println(future.get());
//        }
//        pool.shutdown();
    }

    public void test03() throws InterruptedException, ExecutionException{
        DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyyMMdd");

        Callable<LocalDate> task = () -> {
            LocalDate date = LocalDate.parse("20180428", dtf);
            return date;
        };

        ExecutorService pool = Executors.newFixedThreadPool(10);

        List<Future<LocalDate>> list = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            list.add(pool.submit(task));
        }

        for (Future<LocalDate> future : list) {
            System.out.println(future.get());
        }
        pool.shutdown();
    }


    public static void main(String[] args) {
        DateTimeFormatterTest test = new DateTimeFormatterTest();
        try {
//            test.test01();
//            test.test02();
            test.test03();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }

}
